﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace giaodien
{
    public partial class DN : Form
    {
        static Thread ThreadMoQL;
        static Thread ThreadMoBH;
        public DN()
        {
            InitializeComponent();
        }

        // Khai báo delegate

        public delegate void Gui(string ten,string ma);
        public Gui GuiTen= new Gui(BatDau.LayTen);
        public Gui GuiTenNV = new Gui(QLNhanVien.LayTen);
        public Gui GuiNhanHang = new Gui(QLNhapHang.LayTen);
        public Gui GuiThongKe = new Gui(txttongtien.LayTen);
        public Gui GuiTonKho = new Gui(ThongKeTonKho.LayTen);
        private string ten;
        private string ma;
        
        //Các hoạt động

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            UI.MouseDown(e);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            UI.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            UI.MouseUp();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            UI.Exit(this);
        }


        // Nút chức năng

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (txtId.Text == "" || txtMatKhau.Text == "") { 
                lbKTMatKhau.Text=("Chưa có tên đăng nhập hoặc mật khẩu"); 
            }
            else
            {
                ConnectData obj = new ConnectData();
                DataTable dtt = obj.laydulieuBang("SELECT * FROM NhanVien where tendangnhap='" + txtId.Text + "' and matkhau='" + txtMatKhau.Text + "'");
                if (dtt.Rows.Count!=0 && dtt.Rows[0]["loai"].ToString()=="QL" )
                {
                    this.Width = 480;
                    this.Height = 370;
                    btnQuanLy.Visible = true;
                    btnBanHang.Visible = true;
                    ten = dtt.Rows[0]["tennv"].ToString();
                    ma = dtt.Rows[0]["manv"].ToString();
                    lbKTMatKhau.Text = "";
                }
                else {
                    if (dtt.Rows.Count != 0 && dtt.Rows[0]["loai"].ToString() == "NV")
                    {
                        this.Close();
                        ten = dtt.Rows[0]["tennv"].ToString();
                        ma = dtt.Rows[0]["manv"].ToString();
                        GuiTen(ten,ma);
                        ThreadMoBH.Start();
                    }
                    else {
                        lbKTMatKhau.Text = "Tài khoản hoặc mật khẩu không đúng";
                    }
                }
            }
        }

        private void btnQuanLy_Click(object sender, EventArgs e)
        {
            this.Close();
            GuiTenNV(ten,ma);
            GuiNhanHang(ten,ma);
            ThreadMoQL.Start();
        }

        private void btnBanHang_Click(object sender, EventArgs e)
        {
            this.Close();
            GuiTen(ten,ma);
            ThreadMoBH.Start();
            GuiThongKe(ten, ma);
            GuiTonKho(ten, ma);
        }

       
        
        static void MoQL(){
            Application.Run( new QLNhanVien());
        }
        
        static void MoBH(){
             Application.Run( new BatDau());
        }
        
        
        private void txtId_TextChanged(object sender, EventArgs e)
        {
            this.Width = 480;
            this.Height = 285;
            btnBanHang.Visible = false;
            btnQuanLy.Visible = false;
        }

        private void DN_Load(object sender, EventArgs e)
        {
            this.Width = 480;
            this.Height = 285;
            ThreadMoQL = new Thread(new ThreadStart(MoQL));
            ThreadMoBH = new Thread(new ThreadStart(MoBH));
        }
    }
}
